﻿using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using Microsoft.Win32;

namespace LinqToXml
{
    class LinqToXmlTest
    {
        public void LinqTest()
        {
            FileInfo inputXml = new FileInfo(@"d:\ExampleInput.xml");


            XDocument xmlDoc = XDocument.Load(inputXml.OpenRead());

            var selectedItems = from gadget in xmlDoc.Descendants("Gadget")
                                where gadget.Attribute("type").Value == "Location and maps"
                                select new GadgetInfo()
                                {
                                    Author = gadget.Attribute("author").Value,
                                    Type = gadget.Attribute("type").Value,
                                    Version = gadget.Attribute("version").Value,
                                    Description = gadget.Value
                                };
        }

        public void LinqTestSave()
        {
            //SaveFileDialog saveFile = new SaveFileDialog();
            //if (saveFile.ShowDialog() == true)
            //{
            //    var outputFile = saveFile.OpenFile();
            //    XDocument outputXml = new XDocument(new XElement("Controls", from arControl in settingsCollection
            //                                                                 select new XElement("Control",
            //                                                                     new XAttribute("id", control.OriginalKey),
            //                                                                     new XAttribute("x", control.X),
            //                                                                     new XAttribute("y", control.Y))
            //                                                              )
            //                                     );
            //    outputXml.Save(outputFile);
            //    outputFile.Flush();
            //    outputFile.Close();
            //}
        }

        public void XPathTest()
        {
            FileInfo inputXml = new FileInfo(@"d:\ExampleInput.xml");
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(inputXml.OpenRead());

            var selectedItems = xmlDoc.SelectNodes(@"/Gadgets/Gadget[@type=""Location and maps""]");
        }

    }
}
